#include "stdafx.h"

EnemyMgr::EnemyMgr()
{
}

EnemyMgr::~EnemyMgr()
{
	for (AbstractEnemy * enemy : enemies)
	{
		delete enemy;
	}
}

EnemyMgr * EnemyMgr::getInstance()
{
	static EnemyMgr manager;

	return &manager;
}

bool EnemyMgr::render()
{
	for (AbstractEnemy * enemy : enemies)
	{
		enemy->render();
	}

	return true;
}

bool EnemyMgr::update(double dtime)
{
	while (!spawning.empty() && enemies.size() < maxEnemy)
	{
		AbstractEnemy * enemy = spawning.top();
		enemies.push_back(enemy);
		spawning.pop();

		FrameAnimation * anim = new FrameAnimation(enemy->getX(), enemy->getY());
		anim->setCount(6);
		anim->setFolder("image/animation/flash");
		anim->setInterval(0.03);
		anim->setMap(enemy->getMap());
		AnimationMgr::getInstance()->add(anim);
	}

	std::vector<AbstractEnemy *>::iterator it = enemies.begin();

	while (it != enemies.end())
	{
		if ((*it)->update(dtime))
		{
			it++;
		}
		else
		{
			delete *it;
			it = enemies.erase(it);
		}
	}

	return true;
}

void EnemyMgr::add(AbstractEnemy * enemy)
{
	if (enemies.size() < maxEnemy)
	{
		enemies.push_back(enemy);
	}
	else
	{
		std::cout << "enemy number limit exceeded\n";
	}

	FrameAnimation * anim = new FrameAnimation(enemy->getX(), enemy->getY());
	anim->setCount(6);
	anim->setFolder("image/animation/flash");
	anim->setInterval(0.03);
	anim->setMap(enemy->getMap());
	AnimationMgr::getInstance()->add(anim);
}

bool EnemyMgr::clear()
{
	for (AbstractEnemy * enemy : enemies)
	{
		delete enemy;
	}

	enemies.clear();

	return true;
}